import { defineStore } from "pinia";
import { GetShoppingCartList, getShoppingCartApi } from "../api/shopping";
import { useAppStore } from "../store/user";

export const useShoppingCartStore = defineStore("shoppingCart", {
  state: () => {
    return {
      shoppingCartData: [] as GetShoppingCartList,
      num: 1,
    };
  },
  actions: {
    async getShoppingCartData() {
      const userStore = useAppStore();
      const result = await getShoppingCartApi(userStore.user_id);
      this.shoppingCartData = result.shoppingCartData;
    },
  },
  //计算勾选总价
  getters: {
    allMoney(aa) {
      return aa.shoppingCartData
        .map((item) => {
          let count = 0;

          if (item.check) {
            count += item.num * item.price;
          }
          return count;
        })
        .reduce((old, now) => {
          return old + now;
        }, 0);
    },
    checkArray(state) {
      return state.shoppingCartData.filter((item) => item.check);
    },
  },
});
